<template>
  <div>
    <el-dialog title="新建用户" v-model="dialogVisible" width="30%" center>
      <user-input v-bind="modelConfig" v-model="modelValue" />
      <slot></slot>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handelClick">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, watch } from 'vue'
import userInput from '../userInput/userInput'
import { useStore } from '../../store/store'
export default defineComponent({
  components: {
    userInput
  },
  props: {
    modelConfig: {
      type: Object,
      required: true
    },
    defaultInfo: {
      type: Object,
      default: () => {
        return {}
      }
    },
    otherInfo: {
      type: Object,
      default: () => {
        return {}
      }
    },
    pageName: {
      type: String,
      required: true
    }
  },
  setup(props) {
    const modelValue = ref<any>({})
    const store = useStore()
    watch(
      () => props.defaultInfo,
      (newValue: any) => {
        for (const item of props.modelConfig.formItem) {
          modelValue.value[`${item.field}`] = newValue[`${item.field}`]
        }
      }
    )

    const dialogVisible = ref(false)

    const handelClick = () => {
      dialogVisible.value = false
      if (Object.keys(props.defaultInfo).length) {
        console.log('编辑')
        store.dispatch('system/pathPageAction', {
          pageName: props.pageName,
          editData: { ...modelValue.value, ...props.otherInfo },
          id: props.defaultInfo.id
        })
      } else {
        console.log('新建')
        store.dispatch('system/createPageAction', {
          pageName: props.pageName,
          newData: { ...modelValue.value, ...props.otherInfo }
        })
      }
    }
    return {
      modelValue,
      dialogVisible,
      handelClick
    }
  }
})
</script>

<style scoped></style>
